package com.newly.center.sys.service.loginRecord.impl;

import cn.hutool.core.util.ObjectUtil;
import com.newly.center.sys.service.tenant.api.ITenantService;
import com.newly.common.base.constants.AuthConstant;
import com.newly.common.base.entity.auth.ICurrentTenant;
import com.newly.common.base.entity.sys.tenant.po.TenantPo;
import com.newly.common.core.utils.ConverseUtils;
import com.newly.common.core.utils.RequestUtils;
import com.newly.common.mapper.sys.LoginRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: newly-parent
 * @description: 登录历史基类
 * @author: guan
 * @create: 2022-02-18 10:05
 */
public class AbstractLoginRecordService {

    @Autowired
    protected LoginRecordMapper loginRecordMapper;

    @Autowired
    protected RequestUtils requestUtils;

    @Autowired
    protected ITenantService tenantService;

    protected List<String> getUsernames(ICurrentTenant currentTenant) {
        List<String> entityIds = currentTenant.getEntityIds(AuthConstant.AuthEntityType.TENANT);
        if (ObjectUtil.isEmpty(entityIds)) return null;
        entityIds.remove(currentTenant.getUserId().toString());

        List<TenantPo> tenantPos = tenantService.getByIds(ConverseUtils.toLong(entityIds));
        if (ObjectUtil.isEmpty(tenantPos)) return null;

        return tenantPos.stream().map(TenantPo::getUsername)
                .distinct().collect(Collectors.toList());
    }
}
